<template>
	<view class="outsize">
		<scroll-view scroll-y="true" class="scroll-wrapper">
			<view class="outsizedomn">
				<u-cell-group>
					<u-cell title="开户行名称">
						<template #value>
							<u-input inputAlign='right' placeholder='您的开户行名称' border='none'
								v-model="tableContent.bankName"></u-input>
						</template>
					</u-cell>
					<u-cell title="银行账号">
						<template #value>
							<u-input inputAlign='right' placeholder='您的银行账号' border='none' :cursor-spacing="juli"
								style="position: relative;" v-model="tableContent.bankCard"></u-input>
							<view style="position: absolute;left: 14px;font-size: 10px; color: red;">
								注：上传房源有分润
							</view>
						</template>
					</u-cell>
				</u-cell-group>
			</view>
			<view class="heard">
				<button @click="submit"
					style="border: none;background-color: #FFDA00;border-radius: 10px;font-size: 15px;">
					提交
				</button>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';
	import {
		get,
		post
	} from '@/serve/method.js'
	import {
		urlHead
	} from "@/serve/serve.js" // 全局配置文件
	import {
		test
	} from '@/uni_modules/uview-plus';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	// const borderss = ref(false)
	// const juli = ref(0)
	// onLoad((option) => {
	// 	console.log(option, 999999999)
	// 	tableContent.authStatus = option.id
	// })
	onLoad((res) => {
		let urlone = '/api/user/selectUserOneById'
		uni.getStorage({
			key: 'lifeData',
			success: function(res) {
				get(urlone, {
					userId: res.data.vuex_user.userId,
				}).then((resone) => {
					console.log(resone)
					let one = resone.data
					tableContent.idFrontUrl = one.idFrontUrl
					tableContent.legalValidity = one.legalValidity
					tableContent.bankCard = one.bankCard
					tableContent.bankName = one.bankName
					tableContent.merName = one.merName
					tableContent.address = one.address
					tableContent.type = one.type
					tableContent.idCard = one.idCard
					tableContent.idVersoUrl = one.idVersoUrl
					tableContent.realName = one.realName
					tableContent.phonenumber = one.phonenumber
					front.value = tableContent.legalValidity.substring(0, 8)
					back.value = tableContent.legalValidity.substring(9, 17)
				})
			},
			fail: function(res) {
				if (res.errMsg == 'getStorage:fail data not found') {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}
			}
		})

	})
	const tableContent = reactive({
		representes: '', //是否可以代表房东签约
		legalValidity: '', //证件有效日期
		bankCard: '', //银行卡号
		bankName: '', //开户行名称
		merName: '', //虚拟商户名称
		address: '', //地址
		type: '', //证件类型
		idCard: '', //身份证号
		phonenumber: '', //手机号
		idFrontUrl: '', //身份证正面地址
		idVersoUrl: '', //身份证反面地址
		realName: '', //实际收租人
		authStatus: "2",
		userId: '',
	})
	// 银行卡校验
	const validateBankCard = (cardNumber) => {
		// 简化的Luhn算法检查
		var sum = 0;
		var digit;
		var shouldDouble = false;

		for (var i = cardNumber.length - 1; i >= 0; i--) {
			digit = cardNumber.substring(i, (i + 1));
			digit = parseInt(digit, 10);

			if (shouldDouble) {
				digit *= 2;
				if (digit > 9) {
					digit -= 9;
				}
			}

			sum += digit;
			shouldDouble = !shouldDouble;
		}

		return (sum % 10) === 0;
	}
	// 提交
	const submit = () => {
		if (test.isEmpty(tableContent.bankName)) {
			return wx.showToast({
				title: '请输入您的开户行名称',
				icon: 'none',
				duration: 2000
			})
		}
		if (!validateBankCard(tableContent.bankCard)) {
			return wx.showToast({
				title: '请输入正确的银行卡号',
				icon: 'none',
				duration: 2000
			})
		}
		uni.getStorage({
			key: 'lifeData',
			success: function(res) {
				console.log(tableContent.legalValidity, 888889898)
				tableContent.userId = res.data.vuex_user.userId
				let url = '/api/user/registerUser'
				post(url, tableContent).then((res) => {
					console.log(res, 88888)
					if (res.msg == "身份认证提交审核成功") {
						uni.redirectTo({
							url: '/pages/mine/succer/succer'
						});
					}
				}).fail((res) => {
					console.log(res)
				})
			},
			fail: function(res) {
				if (res.errMsg == 'getStorage:fail data not found') {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}
			}
		})
	}
</script>

<style scoped lang="scss">
	.outsize {
		.scroll-wrapper {
			position: absolute;
			width: calc(100vw - 20px);
			background-color: #F2F2F2;
			padding: 20px 10px;
			height: calc(100vh - 40px);
		}

		.heard {
			margin-left: 5vw;
			color: #999999;
			font-size: 12px;
			// margin: 20px 20px;
			margin: 15px 0;
		}

		.outsizeUp {
			// width: 100%;
			background-color: #ffffff;
			border-radius: 5px;
		}

		.outsizedomn {
			width: 100%;
			background-color: #ffffff;
			border-radius: 5px;
		}

		.outsizedomnOne {
			margin-top: 10px;
			margin-bottom: 10px;
			badding-bottom: 10px;
			width: 100%;
			background-color: #ffffff;
			border-radius: 5px;
		}
	}
</style>